package in.co.codedoc.json;

import java.util.HashSet;

import in.co.codedoc.cg.annotations.HasProperties;
import in.co.codedoc.cg.annotations.IsAJSONMapper;
import in.co.codedoc.cg.annotations.Property;
import in.co.codedoc.json.JSONObjectValue.ForEach;

@IsAJSONMapper
@HasProperties
(
	{
		@Property
		(
			name = "nvps",
			datatypeName = "java.util.HashMap<String,java.util.HashSet<String>>",
			defaultValue = "new java.util.HashMap<String,java.util.HashSet<String>>()"
		)
	}
)
public class Foo3
	extends Foo3Base
{
	@JSONEncoderMethod
	public static JSONObjectValue EnFoo(Foo3 foo3)
	{
		JSONObjectValue json = new JSONObjectValue();
		
		JSONObjectValue nvps = new JSONObjectValue();
		for(String key:foo3.GetNvps().keySet())
		{
			nvps.SetProperty(key, JSONUtil.Encode(foo3.GetNvps().get(key),"java.util.HashSet<String>"));
		}
		
		json.SetProperty("nvps", nvps);
		
		return json;
	}
	
	@JSONDecoderMethod
	public static Foo3 DeFoo(JSONObjectValue jsonForm)
	{
		final Foo3 foo3 = new Foo3();
		
		if
		(
			jsonForm.HasProperty("nvps") 
			&& 
			(jsonForm.GetProperty("nvps") instanceof JSONObjectValue)
		)
		{
			JSONObjectValue nvps = (JSONObjectValue)jsonForm.GetProperty("nvps");

			nvps.ForEach
			(
				new ForEach<Object>()
				{
					@SuppressWarnings("unchecked")
					@Override
					public void Do(int index, JSONValue key, JSONValue value)
					{
						if(value instanceof JSONArrayValue)
						{
							foo3.GetNvps().put
							(
								key instanceof JSONStringValue 
								? 
									((JSONStringValue)key).GetValue()
								:
									key.Encode(),
								(HashSet<String>)JSONUtil.Decode(value, "java.util.HashSet<String>")
							);
						}
					}
				}
			);
		}
		
		
		return foo3;
	}
}
